#include "testdata.h"
#include <QDate>
#include <QDateTime>
#include <QRandomGenerator>

TestData::TestData(QObject *parent) : QObject(parent)
{
    generateData();
}

void TestData::generateData()
{
    // 生成1000条模拟数据
    m_allData.clear();

    QStringList titles = {
        "Qt开发环境搭建", "UI设计最佳实践", "信号槽机制解析", "多线程编程指南",
        "网络通信实现", "数据库操作实战", "自定义控件开发", "Qt样式表高级用法",
        "Model/View架构详解", "Qt国际化实现", "内存管理与优化", "Qt Charts图表应用",
        "Qt Quick开发入门", "设计模式在Qt中的应用", "Qt WebEngine开发", "Qt移动应用开发"
    };

    QStringList statuses = {"待处理", "处理中", "已处理", "已取消"};

    QDate today = QDate::currentDate();

    // 生成1000条记录
    for (int i = 1; i <= 1000; ++i) {
        QMap<QString, QVariant> record;

        // ID
        record["id"] = i;

        // 标题
        int titleIndex = QRandomGenerator::global()->bounded(titles.size());
        record["title"] = titles.at(titleIndex) + QString(" - %1").arg(i);

        // 日期 (过去一年内的随机日期)
        int daysAgo = QRandomGenerator::global()->bounded(1);
        QDate randomDate = today.addDays(-daysAgo);
        record["date"] = randomDate;

        // 状态
        int statusIndex = QRandomGenerator::global()->bounded(statuses.size());
        record["status"] = statuses.at(statusIndex);

        // 操作 (这里存储显示文本)
        record["actions"] = "查看详情";

        m_allData.append(record);
    }
}

QVector<QMap<QString, QVariant>> TestData::getData(
    int page, int pageSize, const QMap<QString, QVariant> &filters,
    const QString &sortField, Qt::SortOrder sortOrder) const
{
    // 应用筛选
    QVector<QMap<QString, QVariant>> filteredData;

    for (const QMap<QString, QVariant> &record : m_allData) {
        bool match = true;

        // 检查每个筛选条件
        for (auto it = filters.begin(); it != filters.end(); ++it) {
            QString field = it.key();
            QVariant filterValue = it.value();

            if (field == "keyword") {
                // 关键字筛选 (应用于标题)
                QString title = record["title"].toString().toLower();
                QString keyword = filterValue.toString().toLower();
                if (!title.contains(keyword)) {
                    match = false;
                    break;
                }
            } else if (field == "date") {
                // 日期范围筛选
                QMap<QString, QVariant> dateRange = filterValue.toMap();
                QDate recordDate = record["date"].toDate();
                QDate startDate = dateRange["start"].toDate();
                QDate endDate = dateRange["end"].toDate();

                if (recordDate < startDate || recordDate > endDate) {
                    match = false;
                    break;
                }
            } else if (field == "status") {
                // 状态筛选
                QString status = record["status"].toString();
                QString filterStatus = filterValue.toString();

                // "全部"选项不筛选
                if (filterStatus != "全部" && status != filterStatus) {
                    match = false;
                    break;
                }
            }
        }

        if (match) {
            filteredData.append(record);
        }
    }

    // 应用排序
    if (!sortField.isEmpty()) {
        std::sort(filteredData.begin(), filteredData.end(),
            [sortField, sortOrder](const QMap<QString, QVariant> &left, const QMap<QString, QVariant> &right) {
                if (sortOrder == Qt::AscendingOrder) {
                    return left[sortField] < right[sortField];
                } else {
                    return left[sortField] > right[sortField];
                }
            });
    }

    // 应用分页
    QVector<QMap<QString, QVariant>> pagedData;
    int startIndex = (page - 1) * pageSize;
    int endIndex = qMin(startIndex + pageSize, filteredData.size());

    for (int i = startIndex; i < endIndex; ++i) {
        pagedData.append(filteredData.at(i));
    }

    return pagedData;
}

int TestData::getTotalCount(const QMap<QString, QVariant> &filters) const
{
    // 与getData类似，但只返回数量
    int count = 0;

    for (const QMap<QString, QVariant> &record : m_allData) {
        bool match = true;

        // 检查每个筛选条件
        for (auto it = filters.begin(); it != filters.end(); ++it) {
            QString field = it.key();
            QVariant filterValue = it.value();

            if (field == "keyword") {
                // 关键字筛选 (应用于标题)
                QString title = record["title"].toString().toLower();
                QString keyword = filterValue.toString().toLower();
                if (!title.contains(keyword)) {
                    match = false;
                    break;
                }
            } else if (field == "date") {
                // 日期范围筛选
                QMap<QString, QVariant> dateRange = filterValue.toMap();
                QDate recordDate = record["date"].toDate();
                QDate startDate = dateRange["start"].toDate();
                QDate endDate = dateRange["end"].toDate();

                if (recordDate < startDate || recordDate > endDate) {
                    match = false;
                    break;
                }
            } else if (field == "status") {
                // 状态筛选
                QString status = record["status"].toString();
                QString filterStatus = filterValue.toString();

                // "全部"选项不筛选
                if (filterStatus != "全部" && status != filterStatus) {
                    match = false;
                    break;
                }
            }
        }

        if (match) {
            count++;
        }
    }

    return count;
}
